package com.ethink.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.constant.CommonRespCode;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.id.generator.IdGenerator;
import com.ethink.order.api.enums.TobSaleReturnOrderReturnStatusEnum;
import com.ethink.order.api.info.TobSaleReturnOrderInfo;
import com.ethink.order.api.model.TobSaleReturnDetailModel;
import com.ethink.order.api.model.TobSaleReturnModeModel;
import com.ethink.order.api.model.TobSaleReturnModel;
import com.ethink.order.api.request.*;
import com.ethink.order.api.response.TobSaleReturnQueryDetailResponse;
import com.ethink.order.common.entity.TobSaleOrder;
import com.ethink.order.common.entity.TobSaleReturnDetailOrder;
import com.ethink.order.common.entity.TobSaleReturnOrder;
import com.ethink.order.common.mapper.TobSaleOrderMapper;
import com.ethink.order.common.mapper.TobSaleReturnDetailOrderMapper;
import com.ethink.order.common.mapper.TobSaleReturnOrderMapper;
import com.ethink.order.service.TobSaleReturnOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 预售退单 Service接口实现
 *
 * @author default
 * @date 2022-12-08
 */
@Slf4j
@Service
public class TobSaleReturnOrderServiceImpl implements TobSaleReturnOrderService {

    @Autowired
    private TobSaleReturnOrderMapper tobSaleReturnOrderMapper;

    @Autowired
    private TobSaleReturnDetailOrderMapper tobSaleReturnDetailOrderMapper;

    @Autowired
    private TobSaleOrderMapper tobSaleOrderMapper;

    @Autowired
    private IdGenerator idGenerator;

    @Override
    public Boolean add(TobSaleReturnOrderAddRequest request) {
        List<TobSaleReturnDetailOrderAddRequest> item = request.getItem().stream().filter(m -> m.getReturnQty() > 0).collect(Collectors.toList());
        if (CollUtil.isEmpty(item)) {
            return null;
        }
        String orderNo = idGenerator.tobtd();
        TobSaleReturnOrder tobSaleReturnOrder = BeanUtil.copyProperties(request, TobSaleReturnOrder.class, "item");
        tobSaleReturnOrder.setOrderNo(orderNo);
        tobSaleReturnOrder.setTakePlace(request.getTakePlace());
        for (TobSaleReturnDetailOrderAddRequest detailOrderAddRequest : item) {
            String detailOrderNo = idGenerator.tobtdxd();
            TobSaleReturnDetailOrder tobSaleReturnDetailOrder = BeanUtil.copyProperties(detailOrderAddRequest, TobSaleReturnDetailOrder.class);
            tobSaleReturnDetailOrder.setDetailOrderNo(detailOrderNo);
            tobSaleReturnDetailOrder.setOrderNo(orderNo);
            tobSaleReturnDetailOrderMapper.insert(tobSaleReturnDetailOrder);
        }
        return tobSaleReturnOrderMapper.insert(tobSaleReturnOrder) > 0;
    }

    @Override
    public Page<TobSaleReturnModel> pageQuery(TobSaleReturnOrderQueryRequest request) {
        return tobSaleReturnOrderMapper.pageQuery(PageUtils.buildPage(request), request);
    }

    @Override
    public TobSaleReturnQueryDetailResponse queryDetail(Long id) {

        TobSaleReturnQueryDetailResponse response = new TobSaleReturnQueryDetailResponse();
        TobSaleReturnOrder tobSaleReturnOrder = tobSaleReturnOrderMapper.selectById(id);
        if (ObjectUtil.isEmpty(tobSaleReturnOrder)) {
            return response;
        }
        TobSaleReturnModel tobSaleReturnModel = BeanUtil.copyProperties(tobSaleReturnOrder, TobSaleReturnModel.class);
        List<TobSaleReturnDetailModel> item = tobSaleReturnDetailOrderMapper.selectDetailByOrderNo(tobSaleReturnOrder.getOrderNo());
        response.setItem(item);
        int sum = item.stream().mapToInt(TobSaleReturnDetailModel::getReturnQty).sum();
        tobSaleReturnModel.setTotalReturnQty(sum);
        // 渠道
        TobSaleOrder tobSaleOrder = tobSaleOrderMapper.selectOne(Wrappers.<TobSaleOrder>lambdaQuery().eq(TobSaleOrder::getOrderNo, tobSaleReturnModel.getTobSaleOrderNo()));
        tobSaleReturnModel.setSaleSubChannel(tobSaleOrder.getSaleSubChannel());
        response.setMain(tobSaleReturnModel);
        return response;

    }

    @Override
    public Boolean update(TobSaleReturnOrderUpdateRequest request) {
        TobSaleReturnOrder tobSaleReturnOrder = tobSaleReturnOrderMapper.selectById(request.getId());
        //先删在增
        tobSaleReturnDetailOrderMapper.delete(new LambdaQueryWrapper<TobSaleReturnDetailOrder>()
                .eq(TobSaleReturnDetailOrder::getOrderNo, tobSaleReturnOrder.getOrderNo()));
        for (TobSaleReturnDetailOrderAddRequest detailOrderAddRequest : request.getItem()) {
            TobSaleReturnDetailOrder tobSaleReturnDetailOrder = BeanUtil.copyProperties(detailOrderAddRequest, TobSaleReturnDetailOrder.class);
            tobSaleReturnDetailOrder.setOrderNo(tobSaleReturnOrder.getOrderNo());
            tobSaleReturnDetailOrder.setDetailOrderNo(idGenerator.tobtdxd());
            tobSaleReturnDetailOrderMapper.insert(tobSaleReturnDetailOrder);
        }
        BeanUtil.copyProperties(request, tobSaleReturnOrder, "item");
        return tobSaleReturnOrderMapper.updateById(tobSaleReturnOrder) > 0;
    }

    @Override
    public Boolean delete(Long id) {
        TobSaleReturnOrder tobSaleReturnOrder = tobSaleReturnOrderMapper.selectById(id);
        if (!TobSaleReturnOrderReturnStatusEnum.S1.getIntValue().equals(tobSaleReturnOrder.getReturnStatus())) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR.getCode(), "仅支持草稿状态删除");
        }
        tobSaleReturnDetailOrderMapper.update(null,
                new LambdaUpdateWrapper<TobSaleReturnDetailOrder>()
                        .eq(TobSaleReturnDetailOrder::getOrderNo, tobSaleReturnOrder.getOrderNo())
                        .set(TobSaleReturnDetailOrder::getIsDeleted, true)
        );
        return tobSaleReturnOrderMapper.delById(id) > 0;
    }

    @Override
    public Boolean pass(Long id) {
        TobSaleReturnOrder tobSaleReturnOrder = tobSaleReturnOrderMapper.selectById(id);
        if (!TobSaleReturnOrderReturnStatusEnum.S2.getIntValue().equals(tobSaleReturnOrder.getReturnStatus())) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR.getCode(), "非法请求");
        }
        UpdateStatusBaseRequest request = new UpdateStatusBaseRequest();
        request.setId(id);
        request.setOrderStatus(TobSaleReturnOrderReturnStatusEnum.S3.getIntValue());
        return updateStatus(request);
    }

    @Override
    public Boolean refuse(Long id) {
        TobSaleReturnOrder tobSaleReturnOrder = tobSaleReturnOrderMapper.selectById(id);
        if (!TobSaleReturnOrderReturnStatusEnum.S2.getIntValue().equals(tobSaleReturnOrder.getReturnStatus())) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR.getCode(), "非法请求");
        }
        UpdateStatusBaseRequest request = new UpdateStatusBaseRequest();
        request.setId(id);
        request.setOrderStatus(TobSaleReturnOrderReturnStatusEnum.S1.getIntValue());
        return updateStatus(request);
    }

    @Override
    public Boolean updateStatus(UpdateStatusBaseRequest request) {
        return tobSaleReturnOrderMapper.update(null, new LambdaUpdateWrapper<TobSaleReturnOrder>()
                .eq(TobSaleReturnOrder::getId, request.getId())
                .set(TobSaleReturnOrder::getReturnStatus, request.getOrderStatus())) > 0;
    }

    @Override
    public Boolean tobSaleReturnOrderStatusFinish(List<String> orderNo) {
        if (CollUtil.isEmpty(orderNo)) {
            return true;
        }
        return tobSaleReturnOrderMapper.update(null, new LambdaUpdateWrapper<TobSaleReturnOrder>()
                .in(TobSaleReturnOrder::getOrderNo, orderNo)
                .set(TobSaleReturnOrder::getReturnStatus, TobSaleReturnOrderReturnStatusEnum.S4.getIntValue())) > 0;
    }

    @Override
    public Boolean tobSaleReturnOrderTakePlaceInsert(TobSaleReturnOrderTakePlaceInsertQueryRequest request) {
        TobSaleReturnOrder entity = new TobSaleReturnOrder();
        BeanUtil.copyProperties(request, entity, true);
        int count = tobSaleReturnOrderMapper.updateById(entity);
        return count > 0;
    }

    @Override
    public TobSaleReturnOrderInfo selectById(Long id) {
        TobSaleReturnOrder tobSaleReturnOrder = tobSaleReturnOrderMapper.selectById(id);
        if (ObjectUtil.isEmpty(tobSaleReturnOrder)) {
            return null;
        }
        TobSaleReturnOrderInfo info = new TobSaleReturnOrderInfo();
        BeanUtil.copyProperties(tobSaleReturnOrder, info, true);
        return info;
    }

    @Override
    public Boolean tobSaleReturnOrderRevokeOrder(Long id) {
        TobSaleReturnOrder tobSaleReturnOrder = new TobSaleReturnOrder();
        tobSaleReturnOrder.setId(id);
        tobSaleReturnOrder.setReturnStatus(TobSaleReturnOrderReturnStatusEnum.S1.getIntValue());
        int count = tobSaleReturnOrderMapper.updateById(tobSaleReturnOrder);
        return count > 0;
    }

    @Override
    public Page<TobSaleReturnModeModel> pageModeQuery(TobSaleReturnOrderQueryRequest request) {
        return tobSaleReturnOrderMapper.pageModeQuery(PageUtils.buildPage(request), request);
    }

}
